package com.sky.com.sky.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sky.constant.StatusConstant;
import com.sky.context.ThreadLocalUtil;
import com.sky.dto.CategoryDTO;

import com.sky.dto.CategoryPageQueryDTO;
import com.sky.entity.Category;
import com.sky.exception.BusinessException;
import com.sky.mapper.CategoryMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.CategoryService;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;


@Service
public class CategoryServiceImpl implements CategoryService {

    @Autowired
    private CategoryMapper categoryMapper;

    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private SetmealMapper setmealMapper;


    //分页查询
    @Override
    public PageResult<Category> getPate(CategoryPageQueryDTO categoryPageQueryDTO) {
        //1. 开启分页查询
        PageHelper.startPage(categoryPageQueryDTO.getPage(),categoryPageQueryDTO.getPageSize());
        //2. 条件查询
        List<Category> list = categoryMapper.getList(categoryPageQueryDTO);
        Page<Category> page = (Page<Category>) list;
        //3. 返回分页对象
        return new PageResult<>(page.getTotal(),page.getResult());

    }

    // 新增分类
    @Override
    public void save(CategoryDTO categoryDTO) {
        //1. 参数校验()
        if(StrUtil.isBlank(categoryDTO.getName()) || categoryDTO.getType() == null ||
        categoryDTO.getSort() == null){
            throw new BusinessException(200,"参数非法!");
        }
        //2. 业务校验(姓名唯一)
        Category oldCategory = categoryMapper.getByName(categoryDTO.getName());
        if(oldCategory != null){
            throw new BusinessException(200,"此分类已存在");
        }
        //3. dto > entity
        Category category = BeanUtil.copyProperties(categoryDTO, Category.class);
        //4. 补充信息
        category.setStatus(StatusConstant.DISABLE);//新增是禁用
        category.setCreateTime(LocalDateTime.now());
        category.setUpdateTime(LocalDateTime.now());
        category.setCreateUser(ThreadLocalUtil.getCurrentId());
        category.setUpdateUser(ThreadLocalUtil.getCurrentId());
        //5.调用mapper新增
        categoryMapper.insert(category);
    }


    /**
     * 根据ID删除分类
     *
     * @param id
     **/
    @Override
    public void deleteById(Long id) {
       //1. 根据分类id统计菜品数量
        Integer dishCount = dishMapper.countByCategoryId(id);
        if(dishCount>0){
            throw new BusinessException(200,"该分类有菜品，无法删除");
        }
        //2. 根据分类id统计套餐数量
        Integer setmealCount = setmealMapper.countByCategoryId(id);
        if(setmealCount>0){
            throw new BusinessException(200,"该分类有套餐,无法删除");
        }
        //3.调用mapper删除
        categoryMapper.deleteById(id);
    }

    /**
     * 根据id修改分类信息
     * @param category
     * **/
    @Override
    public void updateById(Category category) {
        //1. 设置修改人
        category.setUpdateUser(ThreadLocalUtil.getCurrentId());
        //2. 设置最后更新时间
        category.setUpdateTime(LocalDateTime.now());
        //3. 调用Mapper修改
        categoryMapper.update(category);
    }


    /**
     * 分类启用禁用
     * @param status
     * @param id
     * **/
    @Override
    public void startOrStop(Integer status, Long id) {
        Category category = Category.builder()
                .status(status)
                .id(id)
                .updateTime(LocalDateTime.now())
                .updateUser(ThreadLocalUtil.getCurrentId())
                .build();
        categoryMapper.update(category);


    }

    // 根据类型查询分类
    @Override
    public List<Category> getList(Integer type) {
       //1. 将type封装到dto
        CategoryPageQueryDTO categoryPageQueryDTO = new CategoryPageQueryDTO();
        categoryPageQueryDTO.setType(type);
        //2. 调用mapper查询并返回
        return categoryMapper.getList(categoryPageQueryDTO);
    }

    @Override
    public List<Category> getListByStatus(Integer status) {
        return categoryMapper.getListByStatus(status);
    }

    /**
     * 添加分类
     *
     * @param categoryDTO
     **/
    @Override
    public void add(CategoryDTO categoryDTO) {
        //1. 参数校验(唯一索引)
        //2. 业务校验(唯一索引)
        //3. 完成业务
        //3.1 DTO 转 PO
        Category category = BeanUtil.copyProperties(categoryDTO, Category.class);
        //3.2 补全数据
        category.setStatus(StatusConstant.ENABLE);
//        category.setCreateTime(LocalDateTime.now());
//        category.setUpdateTime(LocalDateTime.now());
//        category.setCreateUser(ThreadLocalUtil.getCurrentId());
//        category.setUpdateUser(ThreadLocalUtil.getCurrentId());
        //3.3 保存分类
        categoryMapper.insert(category);
        //4. 返回结果(无)
    }

    /**
     * 根据type类型查询分裂列表
     * @param categoryDTO
     * @return
     * **/
    @Override
    public List<Category> list(CategoryDTO categoryDTO) {
        return categoryMapper.list(categoryDTO);
    }
}
